//
//  DeliveryPartReplaceVC.m
//  ProjectforHDX
//
//  Created by wusiping on 16/8/24.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "DeliveryPartReplaceVC.h"
#import "DeliveryFV.h"
#import "DeliveryHV.h"
#import "DeliveryPartsInfoTC.h"
#import "DeliveryDetailsVC.h"

@interface DeliveryPartReplaceVC ()<UIScrollViewDelegate>
{
    //是否隐藏
    BOOL  open;
    
    UIView *shadowView;        // 阴影

}


/**
 *  用户数据模型
 */
@property (nonatomic, strong) THUserInfoModel *userModel;

//推荐打包件
@property (nonatomic, strong) NSMutableArray *tableArray;
/**
 *  打包件
 */
@property (nonatomic, strong) NSMutableArray *tableParts;

@property (nonatomic, strong) UITableView *tableView;

/**
 *  服务器返回的data总数据
 */
@property (nonatomic, strong) NSMutableDictionary *dataDict;

/**
 *  订单信息
 */
@property (nonatomic, strong) NSMutableDictionary *orderInfoDict;

/**
 *  权限字典
 */
@property (nonatomic, strong) NSMutableArray *permissionsArray;

/**
 *  修改权限
 */
@property (nonatomic, strong) NSString *modifyPermissions;

/**
 *  物流信息
 */
@property (nonatomic, strong) NSMutableDictionary *logisticsInfoDict;


/**
 *  需要发送的板件
 */
@property (nonatomic, strong) NSMutableArray *recommendedArray;

/**
 *  物流公司数组
 */
@property (nonatomic, strong) NSMutableArray *logisCompanyArray;

@end

@implementation DeliveryPartReplaceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.labnav.textColor = [UIColor whiteColor];
    self.labnav.text =[THMangerTool language:@"delivery.title"];// [UtilCheck language:@"setting.consignee"];
    self.mBackColor = [UtilCheck hexStringToColor:@"f6f7fb" alpha:1];
    
    self.userModel=[THMangerTool getAccountInfo];
    
    self.tableArray=[[NSMutableArray alloc] initWithCapacity:0];
    self.tableParts=[[NSMutableArray alloc] initWithCapacity:0];
    open=YES;
    [self  requestDetails];

}

#pragma mark 确认发货信息
- (void)confirmDeliveryData{
    
    NSLog(@"点击了快速发货");
    // 背景色
    shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    shadowView.backgroundColor = [UtilCheck hexStringToColor:@"a5a9aa" alpha:0.7];
    [self.view addSubview:shadowView];
    
    UIView *submitView ;
    
    submitView = [[UIView alloc]initWithFrame:CGRectMake(50, 70, self.view.bounds.size.width - 100, 70 + 40 + 100 + 55)];
        
    submitView.layer.cornerRadius = 5;
    submitView.layer.masksToBounds = YES;
    submitView.backgroundColor = [UtilCheck hexStringToColor:@"eaebec" alpha:1];;
    [shadowView addSubview:submitView];
    
    
    //头部
    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, submitView.bounds.size.width, 70)];
    headView.backgroundColor = [UtilCheck hexStringToColor:@"9ed4e2" alpha:1];
    [submitView addSubview:headView];
    UIImageView *headImageView = [[UIImageView alloc]initWithFrame:CGRectMake((headView.bounds.size.width - 60) / 2, 5, 60, 60)];
    headImageView.image = BDFILEIMAGENAEM(@"Order/img_pack.png");
    [headView addSubview:headImageView];
    
    UIView *defectiveView = [[UIView alloc]initWithFrame:CGRectMake(0, 70, submitView.bounds.size.width, 70 + 40 + 100 + 55 - 60)];
    defectiveView.backgroundColor = [UIColor whiteColor];
    [submitView addSubview:defectiveView];
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, defectiveView.bounds.size.width, defectiveView.bounds.size.height)];
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.alwaysBounceVertical = YES;
    
    CGFloat labelWith = scrollView.width - 20;
    
    UILabel *defectiveLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, labelWith, 20)];
    defectiveLabel.text = @"发货信息确认";
    defectiveLabel.font = [UIFont systemFontOfSize:15];
    [scrollView addSubview:defectiveLabel];

    NSString *addressStr = [self.orderInfoDict objectForKey:@"address"];
    NSString *customerStr = [self.orderInfoDict objectForKey:@"customer"];

    UILabel *contactLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 5 + 20, labelWith, 20)];
    customerStr = [NSString stringWithFormat:@"收货联系人:%@",customerStr];
    contactLab.font = [UIFont systemFontOfSize:13];
    NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:customerStr];
    NSRange range=[[hintString string]rangeOfString:@"收货联系人:"];
    [hintString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:range];
    contactLab.attributedText=hintString;
    [scrollView addSubview:contactLab];

    UILabel *addressLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 5 + 20 *2, labelWith, 20)];
    addressStr = [NSString stringWithFormat:@"收货地址:%@",addressStr];
    addressLab.font = [UIFont systemFontOfSize:13];
    NSMutableAttributedString *hintString2=[[NSMutableAttributedString alloc]initWithString:addressStr];
    NSRange range2=[[hintString2 string]rangeOfString:@"收货地址:"];
    [hintString2 addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:range2];
    addressLab.attributedText=hintString2;
    [scrollView addSubview:addressLab];


    
    
    NSMutableArray *tempArr = [[NSMutableArray alloc]initWithCapacity:0];

    for (int i = 0 ; i < self.tableArray.count ; i ++) {
        NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:self.tableArray[i]];
        NSInteger number = 1;
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        NSString *partInfoStr = [NSString stringWithFormat:@"%@   %@",[tempDict objectForKey:@"partsName"],[tempDict objectForKey:@"partsCode"]];
        [dict setObject:partInfoStr forKey:@"partInfo"];
        [dict setObject:[NSString stringWithFormat:@"%ld", (long)number] forKey:@"partNumber"];

        if (i == 0) {
            [tempArr addObject:dict];
        }else{
            
//            [tempArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                
//            }];
            int flag = -1;

            for (int j = 0 ; j < tempArr.count ; j ++) {
                NSMutableDictionary *tempArrDict = [[NSMutableDictionary alloc]initWithDictionary:tempArr[j]];
                if ([[tempArrDict objectForKey:@"partInfo"] isEqualToString:partInfoStr]) {
                    number = 1 + [[tempArrDict objectForKey:@"partNumber"] integerValue];
                    [dict setObject:[NSString stringWithFormat:@"%ld", (long)number] forKey:@"partNumber"];
                    flag = 0;
                    [tempArr replaceObjectAtIndex:j withObject:dict];
                    
                }
            }
            
            if (flag < 0) {
                [tempArr addObject:dict];
            }
 
        }
    }
    [defectiveView addSubview:scrollView];

    long totalNum = 0;

    for (int i = 0 ; i < tempArr.count ; i ++) {
        UILabel *partInfoLab = [[UILabel alloc]init];
        NSMutableDictionary *tempArrDict = [[NSMutableDictionary alloc]initWithDictionary:tempArr[i]];

        [partInfoLab setFrame:CGRectMake(10, addressLab.bottom + 10 + 20*i , 200, 20)];
        partInfoLab.text = [tempArrDict objectForKey:@"partInfo"];
        partInfoLab.font = [UIFont systemFontOfSize:13];
        partInfoLab.textColor = [UIColor grayColor];
        [scrollView addSubview:partInfoLab];
        
        UILabel *rightNumLab = [[UILabel alloc]init];
        
        [rightNumLab setFrame:CGRectMake(scrollView.right - 20, addressLab.bottom + 10 + 20*i , 20, 20)];
        rightNumLab.text = [tempArrDict objectForKey:@"partNumber"];
        rightNumLab.font = [UIFont systemFontOfSize:13];
        rightNumLab.textColor = [UtilCheck hexStringToColor:@"9ed4e2" alpha:1];
        [scrollView addSubview:rightNumLab];
        totalNum = totalNum + [[tempArrDict objectForKey:@"partNumber"] integerValue];
    }
    
    UILabel *totalCountLab = [[UILabel alloc]initWithFrame:CGRectMake(10, addressLab.bottom + 10 + 20* tempArr.count, 200, 20)];
    totalCountLab.text = [NSString stringWithFormat:@"%@:",[THMangerTool language:@"show.totalCount"]];
    totalCountLab.font = [UIFont systemFontOfSize:13];
    totalCountLab.textColor = [UIColor grayColor];
    [scrollView addSubview:totalCountLab];
    
    UILabel *rightNumLab = [[UILabel alloc]init];
    
    [rightNumLab setFrame:CGRectMake(scrollView.right - 20, addressLab.bottom + 10 + 20* tempArr.count, 20, 20)];
    
    rightNumLab.text = [NSString stringWithFormat:@"%ld",totalNum];
    rightNumLab.font = [UIFont systemFontOfSize:13];
    rightNumLab.textColor = [UtilCheck hexStringToColor:@"9ed4e2" alpha:1];
    [scrollView addSubview:rightNumLab];
    
    scrollView.contentSize = CGSizeMake(scrollView.width, totalCountLab.bottom + 60);
    
    // 确认和修改
    UIView *confirmView = [[UIView alloc]initWithFrame:CGRectMake(0, submitView.bounds.size.height - 38, submitView.bounds.size.width / 2 - 1, 40)];
    confirmView.backgroundColor = [UIColor whiteColor];
    [submitView addSubview:confirmView];
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmButton.frame = CGRectMake((confirmView.bounds.size.width - 80) / 2, (confirmView.bounds.size.height - 20) / 2, 80, 20);
    [confirmButton setTitle:[THMangerTool language:@"DeliveryPartReplaceVC.Confirm"] forState:UIControlStateNormal];
    [confirmButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [confirmButton setTitleColor:[UtilCheck hexStringToColor:@"12a9d1" alpha:1] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(submitFastDelivery) forControlEvents:UIControlEventTouchUpInside];
    [confirmView addSubview:confirmButton];
    
    UIView *modifyView = [[UIView alloc]initWithFrame:CGRectMake(submitView.bounds.size.width / 2, submitView.bounds.size.height - 38, submitView.bounds.size.width / 2, 40)];
    modifyView.backgroundColor = [UIColor whiteColor];
    [submitView addSubview:modifyView];
    
    UIButton *modifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    modifyButton.frame = CGRectMake((confirmView.bounds.size.width - 80) / 2, (confirmView.bounds.size.height - 20) / 2, 80, 20);
    [modifyButton setTitle:[THMangerTool language:@"“DeliveryPartReplaceVC.Back"] forState:UIControlStateNormal];
    [modifyButton setTitleColor:[UtilCheck hexStringToColor:@"12a9d1" alpha:1] forState:UIControlStateNormal];
    [modifyButton.titleLabel setFont:[UIFont systemFontOfSize:15]];

    [modifyButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    [modifyView addSubview:modifyButton];

    
    
}

- (void)back{
    [shadowView removeFromSuperview];

}
#pragma mark 接口请求
//请求初始数据
- (void)requestDetails {
    [[BDHUD sharedManager] showBDHUD:self.view title:@"HUD.title"];
    sleep(0.5);
    [[BDHUD sharedManager] dismissHUD];
    
    NSDictionary *post=[[NSDictionary alloc] initWithObjectsAndKeys:self.orderCode,@"orderCode", nil];
    
    [AFHttpTool postJsonDatawithUrl:@"recommendShipping" parameters:[post mutableCopy] success:^(id responseObject) {
        if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
            if([[responseObject objectForKey:@"data"] isKindOfClass:[NSDictionary class]]){
                NSDictionary *data=[responseObject objectForKey:@"data"];
                
                self.dataDict = [[NSMutableDictionary alloc]initWithDictionary:data];
                [self initView];

            }
            else{
                //返回数据出问题 弹出服务器返回信息
                if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                    [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                }
                else{
                    [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
                }
            }
            
        }
        else{
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
            else{
                [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
            }
            
        }
    } fail:^(id responseObject, NSError *error) {
        [[BDHUD sharedManager] dismissHUD];
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
        
    }];

}


//快速发货请求接口
- (void)submitFastDelivery {
    [shadowView removeFromSuperview];

    [[BDHUD sharedManager] showBDHUD:self.view title:@"HUD.title"];
    sleep(0.5);
    [[BDHUD sharedManager] dismissHUD];
    
    
    NSMutableDictionary *post = [[NSMutableDictionary alloc]init];
    
    if ([[self.orderInfoDict objectForKey:@"type"] integerValue] == 2 && self.logisticsInfoDict != nil) {//替换服务
        post=[[NSMutableDictionary alloc] initWithObjectsAndKeys:self.orderCode,@"orderCode",self.logisticsInfoDict,@"logisticsInfo", nil];
    }else{
        post=[[NSMutableDictionary alloc] initWithObjectsAndKeys:self.orderCode,@"orderCode",nil];

    }
    
    [AFHttpTool postJsonDatawithUrl:@"fastDelivery" parameters:[post mutableCopy] success:^(id responseObject) {
        if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
            if([[responseObject objectForKey:@"data"] isKindOfClass:[NSDictionary class]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else{
                //返回数据出问题 弹出服务器返回信息
                if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                    [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                }
                else{
                    [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
                }
            }
            
        }
        else{
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
            else{
                [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
            }
            
        }
    } fail:^(id responseObject, NSError *error) {
        [[BDHUD sharedManager] dismissHUD];
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
        
    }];
    
}


//安排发货接口
- (void)submitArrangeDelivery {
    [[BDHUD sharedManager] showBDHUD:self.view title:[THMangerTool language:@"HUD.title"]];
    
    
    NSMutableDictionary *post = [[NSMutableDictionary alloc]init];
    

    post=[[NSMutableDictionary alloc] initWithObjectsAndKeys:self.orderCode,@"orderCode",self.recommendedArray,@"parts", nil];
    
    [AFHttpTool postJsonDatawithUrl:@"arrangeDelivery" parameters:[post mutableCopy] success:^(id responseObject) {
        [[BDHUD sharedManager] dismissHUD];

        if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
            if([[responseObject objectForKey:@"data"] isKindOfClass:[NSDictionary class]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                NSLog(@"服务器返回的数据是%@",self.dataDict);
            }
            else{
                //返回数据出问题 弹出服务器返回信息
                if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                    [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                }
                else{
                    [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
                }
            }
            
        }
        else{
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
            else{
                [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
            }
            
        }
    } fail:^(id responseObject, NSError *error) {
        [[BDHUD sharedManager] dismissHUD];

        [[BDHUD sharedManager] dismissHUD];
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
        
    }];
    
}


#pragma mark 数据初始化
-(void)initView{
    
    
    self.orderInfoDict = [[NSMutableDictionary alloc]initWithDictionary:[self.dataDict objectForKey:@"orderInfo"]];
    self.permissionsArray = [[NSMutableArray alloc]initWithArray:[[self.dataDict objectForKey:@"permissions"] objectForKey:@"operationPermissions" ]];
    
    self.modifyPermissions = [NSString stringWithFormat:@"%@",[self.dataDict objectForKey:@"modifyPermissions"]];
    self.logisticsInfoDict = @{@"code":@"",
                               @"com":@"",
                               @"courierID":@""
                               },
    self.recommendedArray = [[NSMutableArray alloc]initWithArray:[self.dataDict objectForKey:@"recommended"]];
    
    

    self.tableArray = [[NSMutableArray alloc]initWithArray:self.recommendedArray];
    
    
    
    
    
    {
        self.tableView = ({
            UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height-60) style:UITableViewStyleGrouped];
            
            [tableView setBackgroundColor:[UtilCheck hexStringToColor:@"ffffff" alpha:1]];
            tableView.showsVerticalScrollIndicator=YES;
            tableView.showsHorizontalScrollIndicator=NO;
            tableView.bounces=NO;
            tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
            tableView.delegate=self;
            tableView.dataSource=self;
            tableView;
        });
        
        if(IOS7){
            [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
        }
        [self.view addSubview:self.tableView];
        
    }
    self.tableView.tableHeaderView=[self createHeaderView];
    
    [self createBottomView];
}


#pragma mark 视图初始化
/**
 *  订单信息视图
 */
-(UIView*)createHeaderView{
    
    DeliveryHV *headerView=[[DeliveryHV alloc] initDHVWithFram:CGRectMake(0, 0, self.tableView.width, 0) OrderInfo:self.orderInfoDict onClick:^{
        
    }];
    return headerView;
}
/**
 *  创建物流信息视图
 */
-(UIView*)createFooterView{
    DeliveryFV *footerView=[[DeliveryFV alloc] initDHVWithFram:CGRectMake(0, 0, self.tableView.width, 90) logistics:self.logisticsInfoDict onClick:^(NSDictionary *dict) {
        
        self.logisticsInfoDict = [dict mutableCopy];
        [self.tableView reloadData];
        
    }];
    return footerView;
}
/**
 *  创建底部按钮
 */
-(void)createBottomView{
    
    NSArray *arr= [self.permissionsArray mutableCopy];

    UIView *btnView=[[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.bottom, self.view.width, 60)];
    //线
    [btnView addSoldLine:1 soldcolor:[UtilCheck hexStringToColor:@"e1e1df" alpha:1] pointA:CGPointMake(0, 0) pointB:CGPointMake(btnView.width,0)];
    [self.view addSubview:btnView];
    
    CGFloat btnwidth=btnView.width / 6;
    CGFloat btnheight=30;
    for(int i=0;i<arr.count;i++){
        
        NSString *title=[THMangerTool operationPermissions:[NSString stringWithFormat:@"%@", [arr objectAtIndex:arr.count - i -1]]];
        RZCustomButtom *submit=[[RZCustomButtom alloc] init];
        [submit setFrame:CGRectMake(btnView.width - (i+1)*(10+btnwidth) ,(btnView.height-btnheight)/2.f,btnwidth,btnheight)];
        [submit setTitle:title forState:UIControlStateNormal];//确认
        submit.tag=100+[[arr objectAtIndex:arr.count - i - 1 ] integerValue];
        NSLog(@"第%d个按钮的tag值是%ld",i,submit.tag);
        [submit addRadius:1 borderwidth:1 bordercolor:[UtilCheck hexStringToColor:@"9ea7ad" alpha:1]];
        [submit setTitleColor:[UtilCheck hexStringToColor:@"9ea7ad" alpha:1]  forState:UIControlStateNormal ];
        [submit.titleLabel setFont:CustomFont(12)];
        [btnView addSubview:submit];
        [submit addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside ];
        
        if(i==0){
            [submit setBackgroundColor:[Toolobject getColorByRGB:26 green:86 blue:188 alpha:1]];
            [submit addRadius:1 borderwidth:1 bordercolor:[Toolobject getColorByRGB:26 green:86 blue:188 alpha:1]];

            [submit setTitleColor:[UtilCheck hexStringToColor:@"ffffff" alpha:1]  forState:UIControlStateNormal ];
        }
    }
    
    
}

#pragma mark 底部按钮点击事件
-(void)btnClick:(RZCustomButtom *)sender{

    long tag = sender.tag - 100;
    switch (tag) {
        case 1://安排发货
        {
            [self submitArrangeDelivery];
        }
            break;
        case 2://快速发货
        {
            [self confirmDeliveryData];
        
        }
            break;
        case 3://发货
        {
            DeliveryDetailsVC *vc = [[DeliveryDetailsVC alloc]init];
            vc.orderCode = self.orderCode;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;

    }
}

#pragma mark tableview代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
        return 90;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(section==0 && [[self.orderInfoDict objectForKey:@"type"] integerValue] == 2){//替换服务才有物流信息
        return  [self createFooterView];
    }
    else{
        return nil;
    }
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, 30)];
    [headerView setBackgroundColor:[UtilCheck getRZColor:244 green:248 blue:251 alpha:1]];
    if(section==0){
        RZCustomLable *labName=[[RZCustomLable alloc] init];
        [labName setFont:CustomFont(14)];
        [labName setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1] ];
        [headerView addSubview:labName];
        
        [labName setFrame:CGRectMake(10, 0, 120, headerView.height)];
        labName.text=[THMangerTool language:@"delivery.delTitle"];
        labName.numberOfLines=1;
        [labName sizeToFit];
        labName.left=10;
        labName.height=headerView.height;
        
        RZCustomButtom *btnOpen=[[RZCustomButtom alloc] init];
        [btnOpen setImage:BDFILEIMAGENAEM(@"wareHouseDeliver/deliverInfoClose") forState:UIControlStateNormal];
        [btnOpen setImage:BDFILEIMAGENAEM(@"wareHouseDeliver/deliverInfoExt") forState:UIControlStateSelected];
        [btnOpen addTarget:self action:@selector(btnOpen:) forControlEvents:UIControlEventTouchUpInside];
        [btnOpen setFrame:CGRectMake(labName.right+5, (headerView.height-20)/2.f, 20, 20)];
        btnOpen.selected=open;
        [headerView addSubview:btnOpen];
        
        
        
        //板件信息
        CustomLabel *labUrgent=[[CustomLabel alloc] initWithFrame:CGRectMake(headerView.right, 0, headerView.width, 300)];
        [labUrgent setFont:CustomFont(14)];
        NSString *string=[NSString stringWithFormat:@"<font color='blue'>%ld</font>",(long)self.tableArray.count];
        NSString *htmlString=[NSString stringWithFormat:[THMangerTool language:@"delivery.parts"],string];
        NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        labUrgent.attributedText=attrStr;
        labUrgent.numberOfLines=1;
        
        CGSize size=[labUrgent  preferredSizeWithMaxWidth:headerView.width];
        labUrgent.width=size.width;
        labUrgent.left=headerView.width-labUrgent.width-10;
        labUrgent.height=headerView.height;
        [headerView addSubview:labUrgent];
    }

    
    return headerView;
}

//显示按钮
-(void)btnOpen:(RZCustomButtom*)sender{
    sender.selected=!sender.selected;
    open=sender.selected;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(open){
        
        return  [self.tableArray count];
    }else{
        return 0;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        
        DeliveryPartsInfoTC *cell= [[DeliveryPartsInfoTC alloc] init];
        cell.data=self.tableArray[indexPath.row];
        
        CGFloat heigt=  [cell createStepView];
        NSLog(@"%f",heigt);
        return heigt;
        
    }
    else{
        return 45;
    }
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DeliveryPartsInfoTC *cell = (DeliveryPartsInfoTC*)[tableView dequeueReusableCellWithIdentifier:@"DeliveryPartsInfoTC"];
    
    if(cell == nil)
    {
        cell = [[DeliveryPartsInfoTC alloc] init];
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    
    cell.data=[self.tableArray objectAtIndex:indexPath.row];
    cell.modifyPermission = self.modifyPermissions;

    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
}


@end
